// pages/periphery/periphery.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
  key: 'J3ZBZ-WWXKJ-57RFY-FR7UP-OMKPK-GMBSJ'
});
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    markers:[]
  },
  onChange(event) {
    var that = this;
    console.log(event)
    that.setData({
      active: event.detail.index,
      type: event.detail.title
    })
    that.nearby_search()
  },
  nearby_search(e) {
    var _this = this;
    // 调用接口
    var iconPath = ['https://api.yipaiju.com/resources/images/wawa/listicon-1.png','https://api.yipaiju.com/resources/images/wawa/listicon-2.png'
    ,'https://api.yipaiju.com/resources/images/wawa/listicon-3.png','https://api.yipaiju.com/resources/images/wawa/listicon-4.png'
    ,'https://api.yipaiju.com/resources/images/wawa/listicon-5.png']
    qqmapsdk.search({
      keyword: _this.data.type, //搜索关键词
      location: _this.data.latitude+','+_this.data.longitude, //设置周边搜索中心点
      success: function (res) { //搜索成功后的回调
        var mks = [];
        mks[0] = _this.data.markers[0]
        for (var i = 1; i < res.data.length; i++) {
          mks.push({ // 获取返回结果，放到mks数组中
            title: res.data[i].title,
            id: res.data[i].id,
            latitude: res.data[i].location.lat,
            longitude: res.data[i].location.lng,
            iconPath: iconPath[_this.data.active], //图标路径
            width: 20,
            height: 20
          })
        }
        _this.setData({ //设置markers属性，将搜索结果显示在地图中
          markers: mks,
          list: res.data
        })
        console.log(res.data,'=====================')
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options)
    var markers = [{
      title: options.address,
      id: 0,
      latitude: options.latitude,
      longitude: options.longitude,
      iconPath:'https://api.yipaiju.com/resources/images/wawa/postion.png',
      width: 20,
      height: 23
    }]
    that.setData({
      active:Number(options.active),
      type:options.type||'公交',
      longitude:options.longitude,
      latitude:options.latitude,
      markers:markers
    })
    that.nearby_search()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  
})